<template>
  <div class="base-container">
    <t-form ref="form" :data="queryParams" label-width="80" @submit="()=>{this.queryParams.pageNum=1;this.pagination.current=1;this.getList()}"
            @reset="()=>{this.queryParams.pageNum=1;this.pagination.current=1;this.getList()}">
      <t-row style="border-bottom: 1px solid #E5E6EB;margin-bottom: 10px">
        <t-col :span="11" class="query-form-inline">
          <t-form-item label="手机号" name="phonenumber">
            <t-input
              clearable
              v-model="queryParams.phonenumber"
              placeholder="请输入手机号"
              style="width: 240px"
            />
          </t-form-item>
          <t-form-item label="发送时间" name="dateRange">
            <t-date-range-picker style="width: 280px" :placeholder="['开始时间','结束时间']" v-model="queryParams.dateRange"  allow-input clearable/>
          </t-form-item>
          <t-form-item label="发送状态" name="status">
            <t-select clearable style="width: 240px" placeholder="状态" :options="[{label:'发送成功',value:'1'},{label:'发送失败',value:'0'}]"
                      v-model="queryParams.status"></t-select>
          </t-form-item>
        </t-col>
        <t-col :span="1" class="operation-container">
          <t-button theme="primary" type="submit">查询</t-button>
          <t-button theme="default" variant="outline" type="reset">重置</t-button>
        </t-col>
      </t-row>
      <t-row style="border-bottom: 1px solid #E5E6EB;margin-bottom: 10px">
        <t-col :span="12" class="">
          <t-button theme="primary" @click="visible = true">发送短信</t-button>
        </t-col>
      </t-row>
    </t-form>
    <t-table
      rowKey="id"
      :data="data"
      :columns="columns"
      hover
      :pagination="pagination"
      :loading="dataLoading"
    >
      <template #sort="{ rowIndex }">
        {{ rowIndex+1 }}
      </template>
      <template #status="{ row }">
        <t-tag theme="success" variant="light" v-if="row.status==='1'">
          发送成功
        </t-tag>
        <t-tag theme="danger" variant="light" v-else-if="row.status==='0'">
          发送失败
        </t-tag>
      </template>
    </t-table>
    <t-dialog
      header="发送短信"
      :visible.sync="visible"
      @confirm="onConfirm"
      width="40%"
      :confirmOnEnter="true"
    >
    <t-form :data="formData" requiredMark :rules="rules" ref="form">
      <t-form-item label="手机号码" name="phonenumber">
        <t-input v-model="formData.phonenumber" placeholder="请输入手机号" />
      </t-form-item>
      <t-form-item label="短信内容" name="content">
        <t-textarea
          v-model="formData.content"
          placeholder="请输入短信内容"
          :autosize="{ minRows: 3, maxRows: 5 }"
        />
      </t-form-item>
      <t-alert theme="info"> 省外手机号暂时接收不到短信！ </t-alert>

    </t-form>
    </t-dialog>

  </div>
</template>

<script>

export default {
  name: "log-index",
  dicts: [],
  components:{
  },
  data(){
    return {
      dialog:{
        header: '',
        objId: null,
        open: false
      },
      formData: {
        phonenumber: null,
        content: '',
      },
      rules: {
        phonenumber: [
          // trigger 默认为 'change'
          { required: true, message: '手机号必填', type: 'error' },
          { whitespace: true, message: '手机号不能为空' },
          { telnumber: true, message: '请输入正确的手机号码' }
        ],
        content: [
          // trigger 默认为 'change'
          { required: true, message: '短信内容必填', type: 'error' },
          { whitespace: true, message: '短信内容不能为空' },
        ],
      },
      data: [],
      visible: false,
      columns: [
        {
          colKey: "sort",
          title: "序号",
          align: "center",
          width: 64
        },
        {
          colKey: "phonenumber",
          title: "手机号",
          align: "center",
        },
        {
          colKey: "content",
          title: "短信内容",
          align: "center",
        },
        {
          colKey: "sendTime",
          title: "发送时间",
          align: "center",
        },
        {
          colKey: "status",
          title: "发送状态",
          align: "center",
        },
      ],
      dataLoading: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        phonenumber: '',
        dateRange: [],
        startSendTime: '',
        endSendTime: '',
        status: '',
      },
      pagination: {
        current: 1,
        pageSize: 10,
        total: 0,
        showPageSize: true,
        pageSizeOptions: [10, 20, 30, 50],
        showFirstAndLastPageBtn: false,
        onChange: (pageInfo) => {
          Object.assign(this.pagination,pageInfo)
          this.queryParams.pageNum = pageInfo.current || 1;
          this.queryParams.pageSize = pageInfo.pageSize;
          this.getList()
        },
      },
    }
  },
  created(){
    this.getList();
  },
  methods:{
    getList(){
      this.dataLoading = true;
      [this.queryParams.startSendTime, this.queryParams.endSendTime] = this.queryParams.dateRange;
      this.$api.system.sms.logList(this.queryParams).then(response => {
        this.pagination.total = response.total;
        this.data = response.rows;
        this.dataLoading = false;
      }).catch((e) => {
        this.dataLoading = false;
        this.$message.error(e.toString());
      });
    },
    onConfirm() {
      this.$refs.form.validate(this.formData).then((res) => {
        if (res === true) {
          this.$api.system.sms.send(this.formData).then((response) => {
            this.visible = false
            this.$message.success('发送成功');
          }).catch((e) => {
            this.$message.error(e.toString());
          })
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
